package com.hbnu.ysc;

import com.hbnu.ysc.dto.UserBasicService;
import com.hbnu.ysc.dto.UserService;
import com.hbnu.ysc.pojo.User;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        System.out.println("============================欢迎来到银行系统============================");

        Scanner sc = new Scanner(System.in);


        while (true){
            System.out.println("");
            System.out.println("-----0、退出系统  1、登录  2、注册-----");
            System.out.println("输入你要操作的编号：");
            int num = sc.nextInt();

            if (num == 0){
                System.out.println("=====================================");
                System.out.println("退出银行系统...");
                break;
            }
            if (num == 1){
                int flag = 3;
                while (flag > 0){
                    System.out.println("=====================================");
                    System.out.println("登录...");
                    System.out.println("请输入用户名：");
                    String name = sc.next();
                    System.out.println("请输入密码：");
                    String password = sc.next();

                    UserBasicService userServer = new UserBasicService();
                    User user = userServer.login(name, password);
                    if (user == null){
                        System.out.println("账号或密码有误！");
                        System.out.println("错误三次将关闭系统");
                        flag--;
                        System.out.println("还剩余" + flag + "次机会");
                        System.out.println("0、退出系统  1、登录  2、返回");
                        int num1 = sc.nextInt();
                        if (num1 == 0){
                            System.out.println("=====================================");
                            System.out.println("退出银行系统...");
                            System.exit(0);
                        }
                        if (num1 == 2){
                            break;
                        }
                    }
                    if (user != null){
                        System.out.println("=====================================");
                        UserService.serve(user);
                    }
                }
            }
            if (num == 2){
                System.out.println("=====================================");
                System.out.println("注册...");
                System.out.println("请输入用户名：");
                String name = sc.next();
                System.out.println("请输入密码：");
                String password = sc.next();

                User user = new User(name, password, 0);
                UserBasicService userServer = new UserBasicService();
                User selectUser = userServer.select(user);
                if (selectUser != null){
                    System.out.println("用户已存在！");
                    continue;
                }
                userServer.createUser(user);

                System.out.println("注册成功！");
            }
        }

    }
}